package edu.jsnu.service.ticket.user.service.impl;

import beans.PageQuery;
import com.github.pagehelper.PageHelper;
import edu.jsnu.service.ticket.user.dao.UserMapper;
import edu.jsnu.service.ticket.user.domain.User;
import edu.jsnu.service.ticket.user.dto.UserDto;
import edu.jsnu.service.ticket.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public List<UserDto> getAllUser(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<UserDto> userList = userMapper.getAllUser();
        return userList;
    }

    @Override
    public User getUserInfoById(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public User getUserInfoByName(String userName) {
        User user = userMapper.selectByName(userName);
        return user;
    }

    @Override
    public Integer saveUser(User user) {
        user.setOperatorId(0);
        user.setOperatorName("");
        user.setOperatorTime(new Date());
        Integer result = userMapper.updateByPrimaryKeySelective(user);
        return result;
    }

    @Override
    public Integer delUser(Integer userId) {
        Integer result = userMapper.deleteByPrimaryKey(userId);
        return result;
    }

    @Override
    public Integer batchDel(Integer[] ids) {
        Map<String, Integer[]> map = new HashMap<>();
        map.put("ids", ids);
        Integer result = userMapper.batchDel(map);
        return result;
    }

    @Override
    public Integer register(String userName, String password) {
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        return null;
    }
}
